//
//  NewsCollectionCell.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/9.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "NewsCollectionCell.h"
#import "NewsCell.h"
#import "PictureCell.h"

@interface NewsCollectionCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation NewsCollectionCell

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.rowHeight = 104;
        [self addSubview:_tableView];
        
        [_tableView registerClass:[NewsCell class] forCellReuseIdentifier:@"newsTableCell"];
        [_tableView registerClass:[PictureCell class] forCellReuseIdentifier:@"PictureCell"];
        
        @weakify(self);
        MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            @strongify(self);
            if (self.footerBlock) {
                [self takeData:self.footerBlock(self.page)];
            }
        }];
        _tableView.mj_footer = footer;
        MJRefreshStateHeader *head = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            @strongify(self);
            if (self.headerBlock) {
                [self takeData:self.headerBlock()];
            }
        }];
        head.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = head;
        
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArr = [NSMutableArray array];
        self.page = 0;
        [self createView];
    }
    return self;
}

- (void)createView {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(self);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 1) {
        NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsTableCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell setModel:self.dataArr[indexPath.row]];
        return cell;
    } else {
        PictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell setModel:self.dataArr[indexPath.row]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.block) {
        self.block(indexPath.row);
    }
}

- (void)takeData:(NSArray *)arr {
    _dataArr = arr;
    [_tableView reloadData];
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
}

@end
